package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.form.ScheduleForm;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.service.ScheduleManageService;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ScheduleManageServiceImpl
  implements ScheduleManageService
{
  private CustomerDAO customerDAO;
  private SaleRecordDAO saleRecordDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;

  public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public ScheduleForm getTodaySchedules(Integer userId, String isViewDept) {
    ScheduleForm scheduleForm = new ScheduleForm();
    Date today = DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd");
    scheduleForm.setDate(today);

    if ((userId == null) || (today == null)) return scheduleForm;
    List saleRecords = new ArrayList();
    if ((!StringUtils.isEmpty(isViewDept)) && ("1".equals(isViewDept)))
    {
      Users user = new Users();
      user = this.userDAO.getUser(userId);
      if (user == null) return scheduleForm;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return scheduleForm;
      saleRecords = this.saleRecordDAO.getSaleRecordsByDeptIds(deptIds);
    }
    else {
      saleRecords = this.saleRecordDAO.getSaleRecordsByCreatePersonId(userId);
    }

    if (saleRecords != null) {
      for (int i = 0; i < saleRecords.size(); i++) {
        SaleRecord saleRecord = new SaleRecord();
        saleRecord = (SaleRecord)saleRecords.get(i);
        if (((saleRecord.getNextContactDate() == null) || (today.compareTo(saleRecord.getNextContactDate()) != 0)) && 
          (today.compareTo(saleRecord.getContactDate()) != 0)) continue;
        if ((saleRecord.getNextContactDate() != null) && (today.compareTo(saleRecord.getNextContactDate()) == 0)) {
          if (saleRecord.getNextContactWayId() != null)
            saleRecord.setContactWayId(saleRecord.getNextContactWayId());
          else {
            saleRecord.setContactWayId(null);
          }
        }
        saleRecord.setCreatePerson(this.userDAO.getUserNameById(saleRecord.getCreatePersonId()));
        Customer customer = new Customer();
        customer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
        if (customer != null) {
          saleRecord.setCustomerName(customer.getCustomerName());
        }
        scheduleForm.getSaleRecords().add(saleRecord);
      }
    }

    return scheduleForm;
  }

  public List getWeekSchedules(Integer userId, String isViewDept) {
    List dateRecords = new ArrayList();

    List dates = new ArrayList();
    dates = DateUtils.getOneWeekDate();
    if ((dates == null) || (dates.size() <= 0)) return dateRecords;
    for (int i = 0; i < dates.size(); i++) {
      Date date = (Date)dates.get(i);
      ScheduleForm scheduleForm = new ScheduleForm();
      scheduleForm.setDate(date);

      if ((userId != null) && (date != null)) {
        List saleRecords = new ArrayList();
        if ((!StringUtils.isEmpty(isViewDept)) && ("1".equals(isViewDept)))
        {
          Users user = new Users();
          user = this.userDAO.getUser(userId);
          if (user != null) {
            String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
            if (!StringUtils.isEmpty(deptIds))
              saleRecords = this.saleRecordDAO.getSaleRecordsByDeptIds(deptIds);
          }
        }
        else
        {
          saleRecords = this.saleRecordDAO.getSaleRecordsByCreatePersonId(userId);
        }
        if (saleRecords != null) {
          for (int j = 0; j < saleRecords.size(); j++) {
            SaleRecord saleRecord = new SaleRecord();
            saleRecord = (SaleRecord)saleRecords.get(j);
            if (((saleRecord.getNextContactDate() == null) || (date.compareTo(saleRecord.getNextContactDate()) != 0)) && 
              (date.compareTo(saleRecord.getContactDate()) != 0)) continue;
            if ((saleRecord.getNextContactDate() != null) && (date.compareTo(saleRecord.getNextContactDate()) == 0)) {
              if (saleRecord.getNextContactWayId() != null)
                saleRecord.setContactWayId(saleRecord.getNextContactWayId());
              else {
                saleRecord.setContactWayId(null);
              }
            }
            saleRecord.setCreatePerson(this.userDAO.getUserNameById(saleRecord.getCreatePersonId()));
            Customer customer = new Customer();
            customer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
            if (customer != null) {
              saleRecord.setCustomerName(customer.getCustomerName());
            }
            scheduleForm.getSaleRecords().add(saleRecord);
          }
        }
      }

      dateRecords.add(scheduleForm);
    }
    return dateRecords;
  }

  public List getMonthSchedules(Integer userId, String isViewDept) {
    List dateRecords = new ArrayList();

    List dates = new ArrayList();
    dates = DateUtils.getCurrentMonthDate();
    if ((dates == null) || (dates.size() <= 0)) return dateRecords;
    for (int i = 0; i < dates.size(); i++) {
      Date date = (Date)dates.get(i);
      ScheduleForm scheduleForm = new ScheduleForm();
      scheduleForm.setDate(date);

      if ((userId != null) && (date != null)) {
        List saleRecords = new ArrayList();
        if ((!StringUtils.isEmpty(isViewDept)) && ("1".equals(isViewDept)))
        {
          Users user = new Users();
          user = this.userDAO.getUser(userId);
          if (user != null) {
            String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
            if (!StringUtils.isEmpty(deptIds))
              saleRecords = this.saleRecordDAO.getSaleRecordsByDeptIds(deptIds);
          }
        }
        else
        {
          saleRecords = this.saleRecordDAO.getSaleRecordsByCreatePersonId(userId);
        }
        if (saleRecords != null) {
          for (int j = 0; j < saleRecords.size(); j++) {
            SaleRecord saleRecord = new SaleRecord();
            saleRecord = (SaleRecord)saleRecords.get(j);
            if (((saleRecord.getNextContactDate() == null) || (date.compareTo(saleRecord.getNextContactDate()) != 0)) && 
              (date.compareTo(saleRecord.getContactDate()) != 0)) continue;
            if ((saleRecord.getNextContactDate() != null) && (date.compareTo(saleRecord.getNextContactDate()) == 0)) {
              if (saleRecord.getNextContactWayId() != null)
                saleRecord.setContactWayId(saleRecord.getNextContactWayId());
              else {
                saleRecord.setContactWayId(null);
              }
            }
            saleRecord.setCreatePerson(this.userDAO.getUserNameById(saleRecord.getCreatePersonId()));
            Customer customer = new Customer();
            customer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
            if (customer != null) {
              saleRecord.setCustomerName(customer.getCustomerName());
            }
            scheduleForm.getSaleRecords().add(saleRecord);
          }
        }
      }

      dateRecords.add(scheduleForm);
    }
    return dateRecords;
  }
}